//
//  ShoppingCarTableViewCell.swift
//  sss
//
//  Created by Mac on 2021/1/13.
//  Copyright © 2021 hrrMac. All rights reserved.
//

import UIKit

class ShoppingCarTableViewCell: UITableViewCell {
    /// 选择按钮
    let selectBtn = UIButton()
    /// 商品图标
    let goodsImgView = UIImageView()
    /// 商品名称
    let goodsNameLab = UILabel()
    /// 商品规格
    let goodsSpecificationLab = UILabel()
    /// 价格
    let priceLab = UILabel()
    let operationView = ShopAddReduceOpertaionView()
    
    /// 全选按钮事件 【是否全选】
    var touchSelectEvent: ((Bool)->Void)?
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        configUI()
    }
    
    private func configUI(){
        // 选择按钮正常状态的图片
        selectBtn.setImage(UIImage(named: "sc_select_no"), for: .normal)
        // 选择按钮选中状态下的图片
        selectBtn.setImage(UIImage(named: "sc_select_yes"), for: .selected)
        selectBtn.imageView?.contentMode = .scaleAspectFit
        selectBtn.addTarget(self, action: #selector(clickSelectBtn), for: .touchUpInside)
        
        goodsImgView.contentMode = .scaleAspectFill
        
        goodsNameLab.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        goodsNameLab.font = UIFont.systemFont(ofSize: 14)
        goodsNameLab.text = ""
        goodsNameLab.numberOfLines = 2
        
        goodsSpecificationLab.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        goodsSpecificationLab.font = UIFont.systemFont(ofSize: 13)
        goodsSpecificationLab.text = "规格："
        
        priceLab.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
        priceLab.font = UIFont.systemFont(ofSize: 17)
        priceLab.text = "¥"
        
        addSubview(selectBtn)
        addSubview(goodsImgView)
        addSubview(goodsNameLab)
        addSubview(goodsSpecificationLab)
        addSubview(priceLab)
        addSubview(operationView)
        
        refreshFrame()
        
    }
    
    // 刷新视图的布局
    private func refreshFrame() {
        
        selectBtn.translatesAutoresizingMaskIntoConstraints = false
        selectBtn.topAnchor.constraint(equalTo: self.topAnchor,constant: 0).isActive = true
        selectBtn.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: 0).isActive = true
        selectBtn.leftAnchor.constraint(equalTo: self.leftAnchor,constant: 0).isActive = true
        selectBtn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        
        goodsImgView.translatesAutoresizingMaskIntoConstraints = false
        goodsImgView.leftAnchor.constraint(equalTo: selectBtn.rightAnchor).isActive = true
        goodsImgView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        goodsImgView.widthAnchor.constraint(equalToConstant: 80).isActive = true
        goodsImgView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        
        goodsNameLab.translatesAutoresizingMaskIntoConstraints = false
        goodsNameLab.leftAnchor.constraint(equalTo: goodsImgView.rightAnchor, constant: 12).isActive = true
        goodsNameLab.rightAnchor.constraint(equalTo: self.rightAnchor,constant: -15).isActive = true
        goodsNameLab.topAnchor.constraint(equalTo: self.topAnchor,constant: 22).isActive = true
        
        goodsSpecificationLab.translatesAutoresizingMaskIntoConstraints = false
        goodsSpecificationLab.leftAnchor.constraint(equalTo: goodsImgView.rightAnchor, constant: 12).isActive = true
        goodsSpecificationLab.rightAnchor.constraint(equalTo: self.rightAnchor,constant: -15).isActive = true
        goodsSpecificationLab.topAnchor.constraint(equalTo: self.topAnchor,constant: 57).isActive = true
        
        priceLab.translatesAutoresizingMaskIntoConstraints = false
        priceLab.leftAnchor.constraint(equalTo: goodsImgView.rightAnchor, constant: 12).isActive = true
//        priceLab.rightAnchor.constraint(equalTo: self.rightAnchor,constant: 15).isActive = true
        priceLab.topAnchor.constraint(equalTo: self.topAnchor,constant: 77).isActive = true
        
        
        operationView.translatesAutoresizingMaskIntoConstraints = false
        operationView.bottomAnchor.constraint(equalTo: self.bottomAnchor,constant: -15).isActive = true
        operationView.rightAnchor.constraint(equalTo: self.rightAnchor,constant: -15).isActive = true
        operationView.heightAnchor.constraint(equalToConstant: 24).isActive = true
        operationView.widthAnchor.constraint(equalToConstant: 78).isActive = true

    }
    
    
    override func layoutSubviews() {
        refreshFrame()
    }
    

}


extension ShoppingCarTableViewCell{
    /// 点击全选的
    @objc func clickSelectBtn() {
        selectBtn.isSelected = !selectBtn.isSelected
        if touchSelectEvent != nil {
            touchSelectEvent!(selectBtn.isSelected)
        }
    }
}
